package com.onpositive.notes.android.tools;

import android.widget.MultiAutoCompleteTextView.Tokenizer;

public class SpaceTokenizer implements Tokenizer {

	public int findTokenStart(CharSequence text, int cursor) {
		int i = cursor;
		
		String str = text.toString().replace("\n", " ") ;
		while (i > 0 && str.charAt(i - 1) != ' ') {
			i--;
		}
		while (i < cursor && str.charAt(i) == ' ') {
			i++;
		}

		return i;
	}

	public int findTokenEnd(CharSequence text, int cursor) {
		
		String str = text.toString().replace("\n", " ") ;
		int i = cursor;
		int len = str.length();

		while (i < len) {
			if (str.charAt(i) == ' ') {
				return i;
			} else {
				i++;
			}
		}

		return len;
	}

	public CharSequence terminateToken(CharSequence text) {
		
		String str = text.toString().replace("\n", " ") ;		
		int l = str.length();
		if(l > 0 && str.charAt( l-1 ) != ' ')
			return str + " " ;

		return str ;
	}

}
